<?php

class LobbyController extends dmq\Controller
{
	protected $useDb = true;

	// Actions.
	const ALL_GAMES = 'All-games';
	const MY_GAMES = 'My-games';
	const CREATE_GAME = 'Create-game';
	const GAME_DETAILS = 'Game-details';

	public function doControll()
	{
		$this->title( 'Lobby' );

		$sectionData = array();

		switch ( $this->view->action() )
		{
			case self::GAME_DETAILS:
				$sectionData['game'] = $this->model->getGame( $this->view->gameId() );

				if ( $this->view->triedToJoinGame() ) {
					try
					{
						$this->model->joinGame( $sectionData['game'], $this->view->auth()->user()->id() );
						$this->model->push("notes", "join", array('message' => $this->view->auth()->user()->name(). ' joined game number '.$this->view->gameId(), 'gameID' => $this->view->gameId()));
						header( 'Location: index.php?c=lobby&a=' . self::GAME_DETAILS . '&game=' . $sectionData['game']->id() );
					}
					catch ( Exception $e )
					{
						$this->view->error( $e->getMessage() );
					}
				}

				if ( $this->view->triedToLeaveGame() ) {
					try
					{
						$this->model->leaveGame( $sectionData['game'], $this->view->playerId(), $this->view->auth()->user()->id() );
						$this->model->push("notes", "leave", array('message' => $this->view->auth()->user()->name(). ' left game number '.$this->view->gameId(), 'gameID' => $this->view->gameId()));
						header( 'Location: index.php?c=lobby&a=' . self::GAME_DETAILS . '&game=' . $sectionData['game']->id() );
					}
					catch ( Exception $e )
					{
						$this->view->error( $e->getMessage() );
					}
				}

				if ( $this->view->triedToRemovePlayer() ) {
					try
					{
						$this->model->removePlayer( $sectionData['game'], $this->view->playerId(), $this->view->auth()->user()->id() );
						$this->model->push("notes", "remove", array('message' => $this->view->auth()->user()->name(). ' was removed from game '.$this->view->gameId().' by '.$this->view->auth()->user()->name(), 'gameID' => $this->view->gameId()));
						header( 'Location: index.php?c=lobby&a=' . self::GAME_DETAILS . '&game=' . $sectionData['game']->id() );
					}
					catch ( Exception $e )
					{
						$this->view->error( $e->getMessage() );
					}
				}

				if ( $this->view->triedToStartGame() ) {
					try
					{
						$this->model->startGame( $sectionData['game'], $this->view->auth()->user()->id() );
						header( 'Location: index.php?c=lobby&a=' . self::GAME_DETAILS . '&game=' . $sectionData['game']->id() );
					}
					catch ( Exception $e )
					{
						$this->view->error( $e->getMessage() );
					}
				}

				break;

			case self::CREATE_GAME:
				if ( $this->view->triedToCreateGame() )
				{
					try
					{
						$this->model->createGame( $this->view->auth()->user()->id(), $this->view->getCreateMaxPlayers(), $this->view->getCreateMapId() );
						header( 'Location: index.php?c=lobby&a=My-games' );
					}
					catch ( Exception $e )
					{
						$this->view->error( $e->getMessage() );
					}
				}

				$sectionData['maps'] = $this->model->getMaps();
				break;

			case self::MY_GAMES:
				$sectionData['games'] = $this->model->filterGames( $this->model->getGames(), array( 'player' => $this->view->auth()->user()->id() ) );
				break;

			case self::ALL_GAMES:
				$sectionData['games'] = $this->model->getGames();
				break;

			default:
				header( 'Location: index.php?c=lobby&a=All-games' );
				break;
		}

		$this->view->buildLobby( $sectionData );

		$this->view->printErrors( '<p class="error"><span>Error :(</span> _ERROR_</p>' . "\r\n" );
		$this->view->printMessages( '<p class="message"><span>Success \o/</span> _MESSAGE_</p>' . "\r\n" );
	}
}
